﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary
{
    /// <summary>
    /// The delegate, which have to be used with a math operator.
    /// </summary>
    public delegate double MathOperator(int index);

    /// <summary>
    /// This class provides special mathematic operators.
    /// </summary>
    [Serializable]
    public static class MathOperators
    {
        /// <summary>
        /// The mathematical sum operator.
        /// </summary>
        /// <param name="startValue">The start value of the sum operator.</param>
        /// <param name="endValue">The end value of the sum operator.</param>
        /// <param name="functionPointer">The function pointer of the function, 
        /// which includes the calculation algorithm of the sum operator.</param>
        /// <returns>The result of the calculation of the sum operator.</returns>
        public static double SumOperator(int startValue, int endValue, MathOperator functionPointer)
        {
            double result = 0;

            for (int i = startValue; i <= endValue; i++)
            {
                result += functionPointer.Invoke(i);
            }

            return result;
        }

        /// <summary>
        /// The mathematical product operator.
        /// </summary>
        /// <param name="startValue">The start value of the product operator.</param>
        /// <param name="endValue">The end value of the product operator.</param>
        /// <param name="functionPointer">The function pointer of the function, 
        /// which includes the calculation algorithm of the product operator.</param>
        /// <returns>The result of the calculation of the product operator.</returns>
        public static double ProductOperator(int startValue, int endValue, MathOperator functionPointer)
        {
            double result = 1;

            for (int i = startValue; i <= endValue; i++)
            {
                result *= functionPointer.Invoke(i);
            }

            return result;
        }
    }
}